package com.cgbl.service.pop

import android.content.Context
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import api.WorkServiceFactory
import com.cgbl.service.R
import com.cgbl.service.adapter.AddTeamMemberAdapter
import com.cgbl.service.adapter.AddTeamTagAdapter
import com.lxj.xpopup.core.BottomPopupView
import com.robot.base.base.BaseView
import com.robot.base.common.api.AbstractDialogSubscriber
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.model.BaseResponse
import com.robot.base.model.MemberAdapterBean
import com.robot.base.util.ToastUtils
import io.reactivex.disposables.Disposable

class FilterPop(
    private val memberIds: List<String>,
    context: Context,
    private val onConfirmListener: (MutableList<MemberAdapterBean>) -> Unit
) : BottomPopupView(context), BaseView {
    private lateinit var recyclerView: RecyclerView

    private lateinit var addTeamTagAdapter: AddTeamMemberAdapter

    override fun getImplLayoutId(): Int {
        return R.layout.app_pop_add_member
    }

    override fun initPopupContent() {
        super.initPopupContent()
        recyclerView = findViewById(R.id.recyclerView)
        addTeamTagAdapter = AddTeamMemberAdapter(true)
        recyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = addTeamTagAdapter
        }
        findViewById<View>(R.id.tv_cancel).setOnClickListener { dismiss() }
        findViewById<View>(R.id.tv_confirm).setOnClickListener {
            val result = addTeamTagAdapter.getSelectedMember()
            onConfirmListener.invoke(result as MutableList<MemberAdapterBean>)
            dismiss()
        }
        fetchHomeMember()
    }

    private fun fetchHomeMember() {
        WorkServiceFactory.getTeamByItemAccount().subscribe(object :
            AbstractDialogSubscriber<BaseResponse<List<MemberAdapterBean>>>(this) {
            override fun onNext(t: BaseResponse<List<MemberAdapterBean>>) {
                addTeamTagAdapter.setNewInstance(t.data as MutableList<MemberAdapterBean>?)
                addTeamTagAdapter.setDefaultCheck(memberIds)
            }
        })
    }

    override fun showLoadingDialog() {

    }

    override fun dismissLoadingDialog() {

    }

    override fun showToast(message: String?) {
        ToastUtils.showShort(message)
    }

    override fun tokenInvalid() {

    }

    override fun addNetworkRequest(d: Disposable?) {

    }

    override fun finishActivity() {
        activity.finish()
    }

}